//
//  ZCMeVC.m
//  zctx
//
//  Created by ZhengDarren on 15/9/28.
//  Copyright © 2015年 sanliang. All rights reserved.
//

#import "ZCMeVC.h"
#import "ZCLoginVC.h"
#import "ZCMeInvolvedVC.h"
#import "ZCMeRaiseVC.h"
#import "ZCMeNotificationCenterVC.h"
@interface ZCMeVC ()<UITableViewDataSource,UITableViewDelegate>{
    UIView *footView;
    UILabel *namelable;
}
@property (nonatomic,strong)UITableView *tableView;
@property (nonatomic,strong)UIImageView *bgImageView;
@end

@implementation ZCMeVC

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.tableView reloadData];
    
    if (IS_IPHONE_4_OR_LESS) {
    
    }
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.navigationItem.title = @"我";
    self.bgImageView = [UIImageView new];
    [self.view addSubview:self.bgImageView];
    [self.bgImageView setImage:[UIImage imageNamed:@"me_bg"]];
    [self.bgImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view).with.insets(UIEdgeInsetsMake(0,0,0,0));
    }];
    
    [self initFootView];//初始化底部FootView
    
    [self initTableView]; //初始化UITableView
  
}

-(void)initFootView{
    footView = [UIView new];
    footView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:footView];
    [footView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.bgImageView.bottom).with.offset(0);
        make.right.equalTo(self.bgImageView).with.offset(0);
        make.width.mas_equalTo(@(MeWeight));
        make.height.mas_equalTo(@130);
    }];
    
//    UIImageView *IconImageView = [UIImageView new];
//    [footView addSubview:IconImageView];
//    IconImageView.userInteractionEnabled = YES;
//    [IconImageView setImage:[UIImage imageNamed:@"me_newhelp_n"]];
//    [IconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.bottom.equalTo(footView.bottom).with.offset(-20);
//        make.centerX.equalTo(footView.mas_centerX);
//        make.width.mas_equalTo(@20);
//        make.height.mas_equalTo(@20);
//    }];
//    
//    UIButton *leftbutton = [UIButton new];
//    [footView addSubview:leftbutton];
//    leftbutton.titleLabel.font = [UIFont systemFontOfSize:13];
//    [leftbutton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
//    [leftbutton addTarget:self action:@selector(ClickHelpButton) forControlEvents:UIControlEventTouchUpInside];
//    [leftbutton setTitle:@"新手帮助" forState:UIControlStateNormal];
//    [leftbutton mas_makeConstraints:^(MASConstraintMaker *make) {
//        make.centerY.equalTo(IconImageView.mas_centerY);
//        make.left.equalTo(IconImageView.right).with.offset(0);
//        make.right.equalTo(footView.right).with.offset(-5);
//        make.height.mas_equalTo(@25);
//    }];
    
    UIImageView *lineView = [UIImageView new];
    [footView addSubview:lineView];
    [lineView setImage:[UIImage imageNamed:@"me_line"]];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(footView.mas_centerX);
        make.bottom.equalTo(footView.top).with.offset(10);
        make.width.mas_equalTo(@(MeWeight-60));
        make.height.mas_equalTo(@1);
    }];
    
}

-(void)initTableView{
    self.tableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [[UIView alloc]init];
    self.tableView.sectionFooterHeight = 0.0001;
    self.tableView.bounces = NO;
    self.tableView.userInteractionEnabled = YES;
    self.tableView.separatorStyle =UITableViewCellSeparatorStyleNone;
    self.tableView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.tableView];

    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.bgImageView.top).with.offset(0);
        make.bottom.equalTo(footView.top).with.offset(0);
        make.left.equalTo(self.bgImageView.left).with.offset(SCREEN_WIDTH-MeWeight);
        make.width.mas_equalTo(@(MeWeight));
    }];
}

#pragma mark UITableViewDataSource,UITableViewDelegate


-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 3;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    static NSString *cellIndentifier = @"wocell";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIndentifier];
    if (cell == nil) {
        cell = [[UITableViewCell alloc]initWithStyle:
                UITableViewCellStyleDefault reuseIdentifier:cellIndentifier];
        cell.backgroundColor = [UIColor clearColor];
        cell.selectedBackgroundView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"me_cell_bg"]];
    }
    cell.textLabel.textColor = [UIColor whiteColor];
    cell.textLabel.textAlignment = NSTextAlignmentCenter;
    if (indexPath.row==0) {
        cell.textLabel.text = @"我发起的";
    }else if (indexPath.row==1){
        cell.textLabel.text = @"我参与的";
    }else{
        cell.textLabel.text = @"通知中心";
    }
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 60;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 210;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if(UserModel.password.length>1){
        if (indexPath.row==0) {
            ZCMeRaiseVC *raiseVC = [[ZCMeRaiseVC alloc]init];
            UINavigationController *nraiseVC= [[UINavigationController alloc]initWithRootViewController:raiseVC];
            [self presentViewController:nraiseVC animated:YES completion:nil];
            
        }else if (indexPath.row==1){
            ZCMeInvolvedVC *involvedeVC = [[ZCMeInvolvedVC alloc]init];
            UINavigationController *ninvolvedVC= [[UINavigationController alloc]initWithRootViewController:involvedeVC];
            [self presentViewController:ninvolvedVC animated:YES completion:nil];
        }else{
            ZCMeNotificationCenterVC *centerVC = [[ZCMeNotificationCenterVC alloc]init];
            UINavigationController *ninvolvedVC= [[UINavigationController alloc]initWithRootViewController:centerVC];
            [self presentViewController:ninvolvedVC animated:YES completion:nil];
        }
    }else{
        [self ClickLoginButton];
    }

}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
 
    UIView * headView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, MeWeight, 210)];
    headView.backgroundColor = [UIColor clearColor];

    UIImageView *IconImageView = [UIImageView new];
    [headView addSubview:IconImageView];
    [IconImageView setImage:[UIImage imageNamed:@"me_tou.png"]];
    [IconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(headView.top).with.offset(30);
        make.centerX.equalTo(headView.mas_centerX);
        make.width.mas_equalTo(@80);
        make.height.mas_equalTo(@80);
    }];
    
    namelable = [UILabel new];
    [headView addSubview:namelable];
    namelable.textColor = [UIColor colorWithHexString:@"#acff00"];
    namelable.font = [UIFont systemFontOfSize:14];
    namelable.textAlignment = NSTextAlignmentCenter;
    
    NSLog(@"usemodel.password = %@",UserModel.password);
    if (![UserModel.password isEqualToString:@"0"]) {
        if (UserModel.mobile.length>1) {
            namelable.text = [NSString stringWithFormat:@"%@",UserModel.mobile];
        }else{
            namelable.text = [NSString stringWithFormat:@"%@",UserModel.username];
        }
    }else{
        [namelable setText:@"亲,您还没有登录哦~"];
    }
    [namelable mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(headView.mas_centerX);
        make.top.equalTo(IconImageView.bottom).with.offset(10);
        make.width.mas_equalTo(@(MeWeight));
        make.height.mas_equalTo(@25);
    }];
    
    UIButton *loginButton = [UIButton new];
    [headView addSubview:loginButton];
    loginButton.backgroundColor = [UIColor clearColor];
    if (![UserModel.password isEqualToString:@"0"]) {
         [loginButton setTitle:@"退出" forState:UIControlStateNormal];
    }else{
        [loginButton setTitle:@"马上登录" forState:UIControlStateNormal];
    }
    [loginButton setBackgroundImage:[UIImage imageNamed:@"me_login_n"] forState:UIControlStateNormal];
    [loginButton addTarget:self action:@selector(ClickLoginButton) forControlEvents:UIControlEventTouchUpInside];
     [loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    loginButton.titleLabel.font = [UIFont systemFontOfSize:14];
    [loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(headView.mas_centerX);
        make.top.equalTo(namelable.bottom).with.offset(10);
        make.width.mas_equalTo(@(80));
        make.height.mas_equalTo(@25);
    }];
    
    UIImageView *lineView = [UIImageView new];
    [headView addSubview:lineView];
    [lineView setImage:[UIImage imageNamed:@"me_line.png"]];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(headView.mas_centerX);
        make.top.equalTo(loginButton.bottom).with.offset(15);
        make.width.mas_equalTo(@(MeWeight-60));
        make.height.mas_equalTo(@1);
    }];
    return headView;
    
}

#pragma mark 登入按钮
-(void)ClickLoginButton{
    
    if (![UserModel.password isEqualToString:@"0"]) {
        [UserModel setMid:@"0"];
        [UserModel setMobile:@"0"];
        [UserModel setPassword:@"0"];
        [self.tableView reloadData];
    }else{
        ZCLoginVC *loginVC = [[ZCLoginVC alloc]init];
        UINavigationController *naloginVC = [[UINavigationController alloc]initWithRootViewController:loginVC];
        [self presentViewController:naloginVC animated:YES completion:^{
        }];
    }
}

#pragma mark 新手帮助
-(void)ClickHelpButton{
    NSLog(@"sdfadsfasdf");
}

@end
